package com.sskj.quantization.presenter;

import com.lzy.okgo.model.Response;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.quantization.bean.QuanBean;
import com.sskj.quantization.http.HttpConfig;
import com.sskj.quantization.ui.fragment.QuantizationFragment;


public class QuantizationFragmentPresenter extends BasePresenter<QuantizationFragment> {


    /**
     * 合约首页
     *
     * @return
     */
    public void getQuanData() {
        httpService.getQuanData().execute(new JsonCallBack<HttpData<QuanBean>>() {
            @Override
            public void onSuccess(Response<HttpData<QuanBean>> response) {
                HttpData<QuanBean> httpData = response.body();
                if (httpData.getStatus() == HttpConfig.OK) {
                    mView.updateUI(httpData.getData());
                }else {
                    mView.refreshFinish();
                }
            }

            @Override
            public void onError(Response<HttpData<QuanBean>> response) {
                mView.refreshFinish();

                super.onError(response);
            }
        });
    }

    /**
     * 购买合约
     *
     * @param tpwd 交易密码
     * @return
     */
    public void buy(String tpwd) {
        httpService.buy(tpwd).execute(new JsonCallBack<HttpData>(this) {
            @Override
            public void onSuccess(Response<HttpData> response) {
                HttpData httpData = response.body();
                ToastUtil.showShort(httpData.getMsg());
                if (httpData.getStatus() == HttpConfig.OK) {

                    mView.buySuccess();
                }
            }
        });
    }

}
